//
// Description: 26. 删除有序数组中的重复项
// Created by Loading on 2024/11/10.
//

#include <bits/stdc++.h>

using namespace std;

int removeDuplicates(vector<int> &nums) {
    // 双指针
    int i = 0, j = 1;
    while (j < nums.size()) {
        // 如果双指针元素不一致，表示探索到了新元素，将前指针的下一个位置赋值，后指针后移
        if (nums[i] != nums[j]) {
            nums[++i] = nums[j++];
        } else {
            ++j;
        }
    }

    return i + 1;
}

int main() {
    vector<int> nums{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
    int res = removeDuplicates(nums);
    cout << res << endl;

    for (int i = 0; i < res; ++i) {
        cout << nums[i] << ' ';
    }
    cout << endl;

    return 0;
}